<template>
  <g-dialog
    width="70%"
    :title="getTitle"
    :visible.sync="dialogVisible"
    :isShowBtn="true"
    append-to-body
    :destroy-on-close="true"
    :beforeClose="hangleOnclose"
  >
    <div v-loading.fullscreen.lock="fullLoading">
      <el-form ref="form" :rules="rules" :model="form" label-width="100px" label-position="top">
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="小队名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入小队名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="小队称号" prop="title">
              <el-input v-model="form.title" placeholder="请输入小队称号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="最大成员数量" prop="memberNum">
              <el-input-number
                :controls="false"
                class="w-full"
                v-model="form.memberNum"
                placeholder="请输入最大成员数量"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="小队等级" prop="level"> </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="12">
          <el-col>
            <el-form-item label="小队描述" prop="desc">
              <el-input type="textarea" :rows="3" v-model="form.title" placeholder="请输入小队称号" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="g-dialog-bottom-action">
      <el-button @click="handleClose" size="small">取消</el-button>
      <el-button type="primary" @click="handleSubmit()" size="small">提交</el-button>
    </div>
  </g-dialog>
</template>

<script>
import { addTaskApi, updateTaskApi, detailTaskApi } from '@/api/task-api';

export default {
  name: 'CreateTask',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    editId: {
      type: [Number, String],
      default: 0
    },
    title: {
      type: String
    }
  },
  data() {
    return {
      localVisible: this.visible,
      fullLoading: false,
      handleThrottledSubmit: null,
      detailData: {},
      form: {
        name: '', // 小队名称
        title: '', // 小队称号
        memberNum: '', // 最大成员数量
        level: '', // 小队等级
        desc: '' // 小队描述
      },
      rules: {
        name: [{ required: true, message: '请输入人员小队名称' }]
      }
    };
  },
  computed: {
    isUpdate() {
      return Boolean(this.editId);
    },
    getTitle() {
      return this.isUpdate ? `编辑${this.title}`:`新增${this.title}}`
    },

    dialogVisible: {
      get() {
        return this.localVisible;
      },
      set(val) {
        this.$emit('update:visible', val);
      }
    }
  },

  mounted() {
    this.getRole();
    if (this.isUpdate) {
      this.getDetail();
    }
  },

  methods: {
    async getRole() {
      try {
        const {
          data: { items }
        } = await getRoleApi();
        this.roleOpts = items ?? [];
      } catch (err) {
        console.warn(err);
      }
    },

    async getDetail() {
      try {
        const { data } = await detailTaskApi(this.editId);
        for (let key in this.form) {
          if (key === 'roles') continue;
          this.form[key] = data[key];
        }

        this.form.roleId = data.roles?.[0].id;
      } catch (err) {
        console.warn(err);
      } finally {
        this.fullLoading = false;
      }
    },

    beforeSubmit() {
      this.form.roles = this.roleOpts.filter((r) => r.id === this.form.roleId);
      const params = {
        ...this.form
      };
      return params;
    },
    handleSubmit() {
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false;

        try {
          this.fullLoading = true;

          let params = this.beforeSubmit();

          let _code = 0;
          if (this.isUpdate) {
            params.id = this.editId;
            const { code } = await updateTaskApi(params);
            _code = code;
          } else {
            const { code } = await addTaskApi(params);
            _code = code;
          }

          if (_code === 200) {
            this.$modal.msgSuccess('操作成功');
            this.handleClose();
          }
        } catch (err) {
          console.warn(err);
        } finally {
          this.fullLoading = false;
        }
      });
    },
    // 点击icon-close触发
    hangleOnclose() {
      this.handleClose();
    },

    handleClose() {
      this.dialogVisible = false;
      this.$emit('close-create');
    },

    changeType(val) {
      this.form.order = 1;
    },
    changeMenu(val) {
      console.log(val);
    },
    changeRole(val) {},
    getFileList(files = []) {
      console.log(files);
      console.log('🚀🚀 ~ getFileList ~ files?.[0] ?? null:', files?.[0]);
      this.form.attachement = files?.[0];
      console.log('🚀🚀 ~ getFileList ~     this.form.attachement:', this.form.attachement);
    }
  }
};
</script>

<style lang="scss" scoped></style>
